package com.lvy.hczn.front.business.service.impl;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.business.mapper.TransferOrderDetailMapper;
import com.lvy.hczn.front.business.mapper.TransferOrderMapper;
import com.lvy.hczn.front.business.service.GoodsBoxDetailService;
import com.lvy.hczn.front.business.service.TransferOrderService;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.common.utils.DateUtils;
import com.lvy.hczn.front.system.domain.business.GoodsBoxDetail;
import com.lvy.hczn.front.system.domain.business.TransferOrder;
import com.lvy.hczn.front.system.domain.business.TransferOrderDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.common.core.text.Convert;

/**
 * 调拨单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-16
 */
@Service
@Slf4j
public class TransferOrderServiceImpl extends ServiceImpl<TransferOrderMapper, TransferOrder> implements TransferOrderService {
    @Autowired
    private TransferOrderMapper transferOrderMapper;

    @Autowired
    private TransferOrderDetailMapper transferOrderDetailMapper;

    /**
     * 查询调拨单
     *
     * @param id 调拨单主键
     * @return 调拨单
     */
    @Override
    public TransferOrder selectTransferOrderById(String id) {
        return transferOrderMapper.selectById(id);
    }

    /**
     * 查询调拨单列表
     *
     * @param transferOrder 调拨单
     * @return 调拨单
     */
    @Override
    public List<TransferOrder> selectTransferOrderList(TransferOrder transferOrder) {
        return transferOrderMapper.selectTransferOrderList(transferOrder);
    }

    /**
     * 新增调拨单
     *
     * @param transferOrder 调拨单
     * @return 结果
     */
    @Override
    public int insertTransferOrder(TransferOrder transferOrder) {
        return transferOrderMapper.insert(transferOrder);
    }

    /**
     * 修改调拨单
     *
     * @param transferOrder 调拨单
     * @return 结果
     */
    @Override
    public int updateTransferOrder(TransferOrder transferOrder) {
        return transferOrderMapper.updateById(transferOrder);
    }

    /**
     * 批量删除调拨单
     *
     * @param ids 需要删除的调拨单主键
     * @return 结果
     */
    @Override
    public int deleteTransferOrderByIds(String ids) {
        String[] arrayId = Convert.toStrArray(ids);
        for (String id : arrayId) {
            transferOrderMapper.deleteById(id);
        }
        return arrayId.length;
    }

    /**
     * 删除调拨单信息
     *
     * @param id 调拨单主键
     * @return 结果
     */
    @Override
    public int deleteTransferOrderById(String id) {
        return transferOrderMapper.deleteById(id);
    }

    /**
     * 根据状态获取调拨单及详情
     *
     * @param transferOrder
     * @return
     * @author yw
     * @date 2023-08-18 14:10:34
     */
    @Override
    public List<TransferOrder> selectIncludeDetailByStatus(TransferOrder transferOrder) {
        List<TransferOrder> transferOrderList = super.list(Wrappers.<TransferOrder>lambdaQuery().eq(TransferOrder::getStatus, transferOrder.getStatus()));
        transferOrderList.forEach(item -> {
            List<TransferOrderDetail> transferOrderDetailList = transferOrderDetailMapper.selectList(Wrappers.<TransferOrderDetail>lambdaQuery().eq(TransferOrderDetail::getTransferCode, item.getTransferCode()));
            item.setDetailList(transferOrderDetailList);
        });
        return transferOrderList;
    }
}
